from tkinter import *
from tkinter import ttk
from db_operations import DBOperations

class StatisticsUI:
    def __init__(self, root):
        self.root = root
        self.db = DBOperations()
        
        self.frame = Frame(self.root, padx=20, pady=20)
        self.frame.pack(expand=True)
        
        # 标签页
        self.notebook = ttk.Notebook(self.frame)
        self.notebook.pack(fill=BOTH, expand=True)
        
        # 单一设备查询
        self.single_tab = Frame(self.notebook)
        self.notebook.add(self.single_tab, text="单一设备查询")
        self.create_single_tab()
        
        # 批量查询
        self.batch_tab = Frame(self.notebook)
        self.notebook.add(self.batch_tab, text="批量查询")
        self.create_batch_tab()
        
        # 报表统计
        self.report_tab = Frame(self.notebook)
        self.notebook.add(self.report_tab, text="报表统计")
        self.create_report_tab()
    
    def create_single_tab(self):
        Label(self.single_tab, text="SN码:").grid(row=0, column=0, sticky=W, pady=5)
        self.single_sn_entry = Entry(self.single_tab)
        self.single_sn_entry.grid(row=0, column=1, pady=5)
        Button(self.single_tab, text="查询", command=self.search_single).grid(row=0, column=2, padx=5)
        
        # 设备详情显示区域
        self.single_info_text = Text(self.single_tab, height=15, width=60)
        self.single_info_text.grid(row=1, column=0, columnspan=3, pady=10)
        
        Button(self.single_tab, text="导出", command=self.export_single).grid(row=2, column=1, pady=20)
    
    def create_batch_tab(self):
        # 查询条件
        Label(self.batch_tab, text="状态:").grid(row=0, column=0, sticky=W, pady=5)
        self.status_combo = ttk.Combobox(self.batch_tab, values=["全部", "在库", "出库", "销库"])
        self.status_combo.grid(row=0, column=1, pady=5)
        self.status_combo.set("全部")
        
        Label(self.batch_tab, text="设备类型:").grid(row=1, column=0, sticky=W, pady=5)
        self.type_combo = ttk.Combobox(self.batch_tab, values=["全部", "BBU", "RRU", "天线", "基带板", "其他"])
        self.type_combo.grid(row=1, column=1, pady=5)
        self.type_combo.set("全部")
        
        Label(self.batch_tab, text="生产厂商:").grid(row=2, column=0, sticky=W, pady=5)
        self.manufacturer_combo = ttk.Combobox(self.batch_tab, values=["全部", "华为", "中兴", "烽火", "其他"])
        self.manufacturer_combo.grid(row=2, column=1, pady=5)
        self.manufacturer_combo.set("全部")
        
        Button(self.batch_tab, text="查询", command=self.search_batch).grid(row=3, column=1, pady=10)
        
        # 设备列表
        columns = ("SN码", "设备型号", "生产厂商", "状态")
        self.batch_tree = ttk.Treeview(self.batch_tab, columns=columns, show="headings")
        for col in columns:
            self.batch_tree.heading(col, text=col)
            self.batch_tree.column(col, width=100)
        self.batch_tree.grid(row=4, column=0, columnspan=3, pady=10)
        
        Button(self.batch_tab, text="导出", command=self.export_batch).grid(row=5, column=1, pady=20)
    
    def create_report_tab(self):
        # 统计报表
        Label(self.report_tab, text="统计报表", font=("Arial", 12)).grid(row=0, column=0, columnspan=3, pady=10)
        
        # 按状态统计
        Label(self.report_tab, text="按状态统计:").grid(row=1, column=0, sticky=W, pady=5)
        self.status_stats_text = Text(self.report_tab, height=3, width=30)
        self.status_stats_text.grid(row=1, column=1, pady=5)
        
        # 按厂商统计
        Label(self.report_tab, text="按厂商统计:").grid(row=2, column=0, sticky=W, pady=5)
        self.manufacturer_stats_text = Text(self.report_tab, height=3, width=30)
        self.manufacturer_stats_text.grid(row=2, column=1, pady=5)
        
        # 按类型统计
        Label(self.report_tab, text="按类型统计:").grid(row=3, column=0, sticky=W, pady=5)
        self.type_stats_text = Text(self.report_tab, height=3, width=30)
        self.type_stats_text.grid(row=3, column=1, pady=5)
        
        # 按制式统计
        Label(self.report_tab, text="按制式统计:").grid(row=4, column=0, sticky=W, pady=5)
        self.standard_stats_text = Text(self.report_tab, height=3, width=30)
        self.standard_stats_text.grid(row=4, column=1, pady=5)
        
        # 本年入库统计
        Label(self.report_tab, text="本年入库统计:").grid(row=1, column=2, sticky=W, pady=5)
        self.inbound_stats_text = Text(self.report_tab, height=3, width=30)
        self.inbound_stats_text.grid(row=1, column=3, pady=5)
        
        # 本年出库统计
        Label(self.report_tab, text="本年出库统计:").grid(row=2, column=2, sticky=W, pady=5)
        self.outbound_stats_text = Text(self.report_tab, height=3, width=30)
        self.outbound_stats_text.grid(row=2, column=3, pady=5)
        
        # 本年销库统计
        Label(self.report_tab, text="本年销库统计:").grid(row=3, column=2, sticky=W, pady=5)
        self.discard_stats_text = Text(self.report_tab, height=3, width=30)
        self.discard_stats_text.grid(row=3, column=3, pady=5)
        
        Button(self.report_tab, text="生成报表", command=self.generate_report).grid(row=5, column=1, pady=20)
        Button(self.report_tab, text="导出报表", command=self.export_report).grid(row=6, column=1, pady=10)
    
    def search_single(self):
        sn = self.single_sn_entry.get()
        if not sn:
            messagebox.showerror("错误", "请输入SN码")
            return
            
        equipment = self.db.get_equipment(sn)
        if equipment:
            self.single_info_text.delete(1.0, END)
            self.single_info_text.insert(END, f"SN码: {equipment[0][2]}\n")
            self.single_info_text.insert(END, f"设备型号: {equipment[0][3]}\n")
            self.single_info_text.insert(END, f"生产厂商: {equipment[0][4]}\n")
            self.single_info_text.insert(END, f"拆除网元: {equipment[0][5]}\n")
            self.single_info_text.insert(END, f"设备类型: {equipment[0][6]}\n")
            self.single_info_text.insert(END, f"状态: {equipment[0][7]}\n")
            self.single_info_text.insert(END, f"制式: {equipment[0][8]}\n")
        else:
            self.single_info_text.delete(1.0, END)
            self.single_info_text.insert(END, "未找到该设备")
    
    def search_batch(self):
        status = self.status_combo.get()
        type_ = self.type_combo.get()
        manufacturer = self.manufacturer_combo.get()
        
        # 清空现有数据
        for item in self.batch_tree.get_children():
            self.batch_tree.delete(item)
        
        # 获取设备列表
        equipment_list = self.db.get_equipment()
        
        # 过滤结果
        for eq in equipment_list:
            if (status == "全部" or eq[7] == status) and \
               (type_ == "全部" or eq[6] == type_) and \
               (manufacturer == "全部" or eq[4] == manufacturer):
                self.batch_tree.insert("", END, values=(eq[2], eq[3], eq[4], eq[7]))
    
    def generate_report(self):
        stats = self.db.get_statistics()
        
        # 更新状态统计
        self.status_stats_text.delete(1.0, END)
        for status, count in stats['status'].items():
            self.status_stats_text.insert(END, f"{status}: {count}\n")
        
        # 更新厂商统计
        self.manufacturer_stats_text.delete(1.0, END)
        for manu, count in stats['manufacturer'].items():
            self.manufacturer_stats_text.insert(END, f"{manu}: {count}\n")
        
        # 更新类型统计
        self.type_stats_text.delete(1.0, END)
        for type_, count in stats['type'].items():
            self.type_stats_text.insert(END, f"{type_}: {count}\n")
        
        # 更新制式统计
        self.standard_stats_text.delete(1.0, END)
        for std, count in stats['standard'].items():
            self.standard_stats_text.insert(END, f"{std}: {count}\n")
            
        # 更新本年入库统计
        self.inbound_stats_text.delete(1.0, END)
        for manu, type_, std, count in stats['inbound']:
            self.inbound_stats_text.insert(END, f"{manu}-{type_}-{std}: {count}\n")
            
        # 更新本年出库统计
        self.outbound_stats_text.delete(1.0, END)
        for manu, type_, std, count in stats['outbound']:
            self.outbound_stats_text.insert(END, f"{manu}-{type_}-{std}: {count}\n")
            
        # 更新本年销库统计
        self.discard_stats_text.delete(1.0, END)
        for manu, type_, std, count in stats['discard']:
            self.discard_stats_text.insert(END, f"{manu}-{type_}-{std}: {count}\n")
    
    def export_single(self):
        # 模拟导出功能
        messagebox.showinfo("提示", "导出功能将在后续实现")
    
    def export_batch(self):
        # 模拟导出功能
        messagebox.showinfo("提示", "导出功能将在后续实现")
    
    def export_report(self):
        # 模拟导出功能
        messagebox.showinfo("提示", "导出功能将在后续实现")